package com.fumihiko.modules.wsbj.service.impl;

import com.fumihiko.common.utils.*;
import com.fumihiko.modules.ca.vo.UserTokenVo;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.fumihiko.modules.wsbj.dao.TbTokenDao;
import com.fumihiko.modules.wsbj.entity.TbTokenEntity;
import com.fumihiko.modules.wsbj.service.TbTokenService;


@Service("tbTokenService")
public class TbTokenServiceImpl extends ServiceImpl<TbTokenDao, TbTokenEntity> implements TbTokenService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TbTokenEntity> page = this.page(
                new Query<TbTokenEntity>().getPage(params),
                new QueryWrapper<TbTokenEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public String getToken(String appId, String appSecret, String timestamp) throws ParseException {
        String token = TokenProccessor.getInstance().makeToken().replaceAll("\\+","");
        TbTokenEntity tbTokenEntity = new TbTokenEntity();
        tbTokenEntity.setAppId(appId);
        tbTokenEntity.setAppSecret(appSecret);
        tbTokenEntity.setAccessToken(token);
        String data = DateTimeUtil.getChangeDateByHour(DateTimeUtil.getCurrentTime(), SxConstants.HOUR);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        tbTokenEntity.setExpirationTime(formatter.parse(data));
        this.save(tbTokenEntity);

        return token;
    }

    @Override
    public TbTokenEntity getUserTokenVoObjByAccessToken(String accessToken) {
        return baseMapper.getUserTokenVoObjByAccessToken(accessToken);
    }

}
